package com.shop2cn.iapi.sellerpromotion.testcase;

import java.io.IOException;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sellerpromotion.parameter.CreatePromotionBean;
import com.shop2cn.iapi.sellerpromotion.parameter.PromotionEndBean;
import com.shop2cn.iapi.sellerpromotion.service.PromotionEndCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.Shop2cnSellerPromotionCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.mongo.Shop2cnSellerPromotionInfoWapper;
import com.ymttest.utils.YMTDateUtil;
/**
*结束满减
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_PromotionEnd{
	private static PromotionEndBean promotionendBean;
	private static PromotionEndCall promotionendCall;
	private static Integer mchId;
	private static Shop2cnSellerPromotionInfoWapper Shop2cnSellerPromotionInfoWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("结束满减");
		try {
			Shop2cnSellerPromotionInfoWapper=new Shop2cnSellerPromotionInfoWapper();
			mchId=GodmchManageCallService.CreateSellerMch();
		} catch (Exception e) {
		}
	}

	@Before
	public void caseUp() {
		promotionendBean=new PromotionEndBean();
		promotionendCall=new PromotionEndCall();
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(promotionendCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
		try {
			Shop2cnSellerPromotionCallService.EndAllEnablePromotion(mchId);
		} catch (IOException e) {
//			e.printStackTrace();
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_PromotionEnd_001() {
		Logger.start(true,"未开始结束满减成功");
		try {
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(mchId, 1);
			createpromotionBean.setBegin(YMTDateUtil.getBeforeOrNextDay(1,"yyyy-MM-dd HH:mm:ss"));
			createpromotionBean.setEnd(YMTDateUtil.getBeforeOrNextDay(2,"yyyy-MM-dd HH:mm:ss"));
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			promotionendBean.setPromotionId(promotionId);
			promotionendBean.setSellerId(mchId); 
			promotionendCall.setData(promotionendBean);
			promotionendCall.callService();			
			Logger.verifyEquals("200", promotionendCall.getString("code"), "验证返回");
			Map m = Shop2cnSellerPromotionInfoWapper.getPromotionById(promotionId);
			Logger.debug(m);
			Logger.verifyNotEquals(createpromotionBean.getEnd().substring(0, 16),
					YMTDateUtil.getFromDTSDate(m.get("et").toString(), YMTDateUtil.YMDHMS).substring(0, 16), "验证et发生变化");
			Integer status = Shop2cnSellerPromotionCallService.getStatus(promotionId);
			Logger.verifyEquals(2, status, "验证状态");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_PromotionEnd_002() {
		Logger.start(true,"不输入sellerid失败");
		try {
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(mchId, 1);
			createpromotionBean.setBegin(YMTDateUtil.getBeforeOrNextDay(1,"yyyy-MM-dd HH:mm:ss"));
			createpromotionBean.setEnd(YMTDateUtil.getBeforeOrNextDay(2,"yyyy-MM-dd HH:mm:ss"));
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			promotionendBean.setPromotionId(promotionId);
//			promotionendBean.setSellerId(mchId); 
			promotionendCall.setData(promotionendBean);
			promotionendCall.callService();			
			Logger.verifyEquals("400", promotionendCall.getString("code"), "验证返回");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_PromotionEnd_003() {
		Logger.start(true,"不输入promotionId失败");
		try {
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(mchId, 1);
			createpromotionBean.setBegin(YMTDateUtil.getBeforeOrNextDay(1,"yyyy-MM-dd HH:mm:ss"));
			createpromotionBean.setEnd(YMTDateUtil.getBeforeOrNextDay(2,"yyyy-MM-dd HH:mm:ss"));
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
//			promotionendBean.setPromotionId(promotionId);
			promotionendBean.setSellerId(mchId); 
			promotionendCall.setData(promotionendBean);
			promotionendCall.callService();			
			Logger.verifyEquals("400", promotionendCall.getString("code"), "验证返回");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_PromotionEnd_004() {
		Logger.start(true,"输入sellerid-promotionId不匹配失败");
		try {
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(mchId, 1);
			createpromotionBean.setBegin(YMTDateUtil.getBeforeOrNextDay(1,"yyyy-MM-dd HH:mm:ss"));
			createpromotionBean.setEnd(YMTDateUtil.getBeforeOrNextDay(2,"yyyy-MM-dd HH:mm:ss"));
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			promotionendBean.setPromotionId(promotionId);
			promotionendBean.setSellerId(1); 
			promotionendCall.setData(promotionendBean);
			promotionendCall.callService();			
			Logger.verifyEquals("300", promotionendCall.getString("code"), "验证返回");
			//{"code":300,"message":"促销活动不存在","data":null}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_PromotionEnd_005() {
		Logger.start(true,"已开始结束满减成功");
		try {
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(mchId, 1);
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
               
			promotionendBean.setPromotionId(promotionId);
			promotionendBean.setSellerId(mchId); 
			promotionendCall.setData(promotionendBean);
			promotionendCall.callService();			
			Logger.verifyEquals("200", promotionendCall.getString("code"), "验证返回");
			Map m = Shop2cnSellerPromotionInfoWapper.getPromotionById(promotionId);
			Logger.debug(m);
			Logger.verifyNotEquals(createpromotionBean.getEnd().substring(0, 16),
					YMTDateUtil.getFromDTSDate(m.get("et").toString(), YMTDateUtil.YMDHMS).substring(0, 16), "验证et发生变化");
			Integer status = Shop2cnSellerPromotionCallService.getStatus(promotionId);
			Logger.verifyEquals(2, status, "验证状态");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_PromotionEnd_006() {
		Logger.start(true,"重复结束满减-数据库结果正确");
		try {
			CreatePromotionBean createpromotionBean = Shop2cnSellerPromotionCallService.initCreatePromotionBean(mchId, 1);
			Integer promotionId = Shop2cnSellerPromotionCallService.CreatepromotionCall(createpromotionBean);
			promotionendBean.setPromotionId(promotionId);
			promotionendBean.setSellerId(mchId); 
			promotionendCall.setData(promotionendBean);
			promotionendCall.callService();			
			promotionendCall.callService();			
			Logger.verifyEquals("200", promotionendCall.getString("code"), "验证返回");
			Map m = Shop2cnSellerPromotionInfoWapper.getPromotionById(promotionId);
			Logger.debug(m);
			Logger.verifyNotEquals(createpromotionBean.getEnd().substring(0, 16),
					YMTDateUtil.getFromDTSDate(m.get("et").toString(), YMTDateUtil.YMDHMS).substring(0, 16), "验证et发生变化");
			Integer status = Shop2cnSellerPromotionCallService.getStatus(promotionId);
			Logger.verifyEquals(2, status, "验证状态");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}